var salt = sessionStorage.getItem('usersalt');
var maxEstimateNum = 0.01;

var itemArr = [{
	isShow: 0,
	name: "起重伤害",
	describeModels: [{
		isShow: 1,
		probability: "",
		probabilityNum: 0.1,
		frequency: "",
		frequencyNum: 0.1,
		aftermath: "",
		aftermathNum: 1,
		estimateNum: 0.01,
		riskInfos: [{
			isShow: 1,
			type: "人的不安全行为",
			analyse: "",
			measure: "安全管理措施",
			specific: "",
			ent: "厂",
			responsible: ""
		}]
	}]
}];

layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;

	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}

	laydate.render({
		elem: '#dexisionTime',
		trigger: 'click'
	});
});

var checkId = getQueryVariable("id");
var editCompanyId;

/*------------单位节点树.start------------*/
function showUnitTree() {
	$('#applyUnitTree').show();
}

var company;
var arr = new Array();

getAllCompany();

var setting = {
	data: {
		simpleData: {
			enable: true
		}
	},
	check: {
		enable: true,
		chkStyle: "radio", //单选框
		radioType: "all" //对所有节点设置单选
	},
	callback: {
		onCheck: zTreeOnCheck
	}
};

$.fn.zTree.init($("#applyUnitTree"), setting, company);

function zTreeOnCheck(event, treeId, treeNode) {
	var checked = treeNode.checked;
	if(checked) {
		$('#applyUnit').val(treeNode.name);
	} else {
		$('#applyUnit').val("");
	}
	$('#applyUnitTree').hide();
};

function getTreeVal() {
	var unitTreeVal = [];
	var treeObj = $.fn.zTree.getZTreeObj("applyUnitTree");
	var nodes = treeObj.getCheckedNodes(true);
	for(var i = 0; i < nodes.length; i++) {
		unitTreeVal.push(nodes[i].id); //获取选中节点的值
	}
	return unitTreeVal;
}
/*------------单位节点树.end------------*/

/*-------------编辑获取值.start-------------*/
if(checkId != false) {
	$("#pageTitle").html("编辑风险分级清单");
	$('#id').val(checkId);
	$.ajax({
		url: url + 'risk/getInfo',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			Token: salt,
			id: checkId
		},
		success: function(data) {
			if(data.code == 0) {
				initEditItem(data.data);
			} else {
				setTimeout(function() {
					layer.msg(data.msg);
				}, 1000);
				checkId = false;
				initItemShow();
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
} else {
	$("#pageTitle").html("新增风险分级清单");
}

/*-------------元素位置.start-------------*/
function setPosition() {
	var textareaHeight = $("textarea").height();
	$(".ctddel").height(textareaHeight + 2); //2为textarea线的高度
	$(".ctdadd").height(textareaHeight + 2);
	$(".cti-add").css("margin-top", textareaHeight - 16);
}
/*-------------元素位置.end-------------*/

function set_select_checked(selectId, checkValue) {
	var obj = document.getElementById(selectId).options;
	for(i = 0; i < obj.length; i++) {
		if(obj[i].value == checkValue)
			obj[i].selected = true;
	}
}

//页面检查项目框显示
function initItemShow(isaddContent) {
	var tbodyHtml = "";
	for(var i = 0; i < itemArr.length; i++) {
		if(itemArr[i].isShow == 1) {
			var rightTab = 1;
			var contenttab = "";
			//内容框list
			for(var j = 0; j < itemArr[i].describeModels.length; j++) {
				var contentHtml = ""; //检查项目内容html
				if(itemArr[i].describeModels[j].isShow == 1) {
					for(var z = 0; z < itemArr[i].describeModels[j].riskInfos.length; z++) {
						if(itemArr[i].describeModels[j].riskInfos[z].isShow == 1) {
							contentHtml +=
								"<tr class=\"green-box\" id=\"item-content-" + i + "-" + j + "-" + z + "\">" +
								"<td colspan=\"2\" style=\"padding-right: 0px;padding-left: 8%;\">" +
								"<div class=\"layui-col-md12 ctd ctd1 sectd\">" +
								"<div class=\"layui-col-md12 ctdd\">" +
								"<div class=\"layui-col-md2 ctdlb\"><label>风险分类</label></div>" +
								"<select lay-ignore class='layui-input layui-unselect' id=\"item-type-" + i + "-" + j + "-" + z + "-1\" onchange=\"setItemValue('item-type-" + i + "-" + j + "-" + z + "-1'," + i + "," + j + "," + z + ", 2,2,'type')\">" +
								"<option value=\"人的不安全行为\">人的不安全行为</option>" +
								"<option value=\"物的不稳定因素\">物的不稳定因素</option>" +
								"<option value=\"管理缺陷\">管理缺陷</option>" +
								"<option value=\"环境因素\">环境因素</option>" +
								"</select>" +
								"</div>" +
								"<div class=\"layui-col-md12 ctdd\">" +
								"<div class=\"layui-col-md2 ctdlb\"><label>措施分类</label></div>" +
								"<select lay-ignore class='layui-input layui-unselect' id=\"item-measure-" + i + "-" + j + "-" + z + "-1\" onchange=\"setItemValue('item-measure-" + i + "-" + j + "-" + z + "-1'," + i + "," + j + "," + z + ", 2,2,'measure')\">" +
								"<option value=\"安全管理措施\">安全管理措施</option>" +
								"<option value=\"安全技术措施\">安全技术措施</option>" +
								"</select>" +
								"</div>" +
								"<div class=\"layui-col-md12\">" +
								"<div class=\"layui-col-md2 ctdlb\"><label>责任单位</label></div>" +
								"<select lay-ignore class='layui-input layui-unselect' id=\"item-ent-" + i + "-" + j + "-" + z + "-1\" onchange=\"setItemValue('item-ent-" + i + "-" + j + "-" + z + "-1'," + i + "," + j + "," + z + ",2,2,'ent')\">" +
								"<option value=\"厂\">厂</option>" +
								"<option value=\"处理站\">处理站</option>" +
								"<option value=\"班组\">班组</option>" +
								"</select>" +
								"</div>" +
								"</div>" +
								"</td>" +
								"<td colspan=\"2\" style=\"padding-left: 0px;padding-right: 0px;\">" +
								"<div class=\"layui-col-md12 ctd ctd2\">" +
								"<div class=\"layui-col-md11 ctdd\">" +
								"<div class=\"layui-col-md2 ctdlb\">" +
								"<label>风险分析</label>" +
								"</div>" +
								"<input type=\"text\" class=\"layui-input layui-input1\" placeholder=\"输入风险分析\" id=\"item-analyse-" + i + "-" + j + "-" + z + "-1\" onchange=\"setItemValue('item-analyse-" + i + "-" + j + "-" + z + "-1'," + i + "," + j + "," + z + ",2,1,'analyse')\" value=\"" + itemArr[i].describeModels[j].riskInfos[z].analyse + "\" />" +
								"</div>" +
								"<div class=\"layui-col-md11 ctdd\">" +
								"<div class=\"layui-col-md2 ctdlb\">" +
								"<label>具体措施</label>" +
								"</div>" +
								"<input type=\"text\" class=\"layui-input layui-input1\" placeholder=\"输入所需采取的措施\" id=\"item-specific-" + i + "-" + j + "-" + z + "-1\" onchange=\"setItemValue('item-specific-" + i + "-" + j + "-" + z + "-1'," + i + "," + j + "," + z + ", 2,1,'specific')\" value=\"" + itemArr[i].describeModels[j].riskInfos[z].specific + "\" />" +
								"</div>" +
								"<div class=\"layui-col-md11\">" +
								"<div class=\"layui-col-md2 ctdlb\">" +
								"<label>责任人</label>" +
								"</div>" +
								"<input type=\"text\" class=\"layui-input layui-input1\" placeholder=\"输入责任人\"  id=\"item-responsible-" + i + "-" + j + "-" + z + "-1\"  onchange=\"setItemValue('item-responsible-" + i + "-" + j + "-" + z + "-1'," + i + "," + j + "," + z + ", 2,1,'responsible')\" value=\"" + itemArr[i].describeModels[j].riskInfos[z].responsible + "\" />" +
								"</div>" +
								"<div class=\"layui-col-md1\" style=\"position: absolute;right: 5px;\">" +
								"<div class=\"ctdd ctddel\" style=\"margin-bottom: 129px; height: 2px;\">" +
								"<i class=\"cti cti-del\" onclick=\"delContent(" + i + "," + j + "," + z + ")\"></i>" +
								"</div>" +
								"<div class=\"ctdadd\" style =\"height: 2px;\">" +
								"<i class=\"cti cti-add\" onclick=\"addContent(" + i + "," + j + "," + z + ")\"  style=\"margin-top: -16px;\"></i>" +
								"</div>" +
								"</div>" +
								"</div>" +
								"</td>" +
								"</tr>";
							//按钮合并行数+1
							rightTab = itemArr[i].describeModels[j].riskInfos.length + 2
						}
					}
					if(j == 0) {
						contenttab +=
							"<tr id=\"item-probability-title-" + i + "-" + j + "\">" +
							"<td>" +
							"<div class=\"layui-input-inline ftdl\">" +
							"<label class=\"fowei\">可能性分析</label></div>" +
							"</td>" +
							"<td style=\"padding-right: 0px;\">" +
							"<input type=\"text\" class=\"layui-input layui-input1\" id=\"item-probability-" + i + "-" + j + "-1\" value=\"" + itemArr[i].describeModels[j].probability + "\" placeholder=\"输入检查项目名称\" onchange=\"setItemValue('item-probability-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,1,'probability')\"> " +
							"<select lay-ignore class='layui-input layui-unselect' id=\"item-probabilityNum-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-probabilityNum-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'probabilityNum')\">" +
							"<option value=\"0.1\">0.1</option>" +
							"<option value=\"0.2\">0.2</option>" +
							"<option value=\"0.5\">0.5</option>" +
							"<option value=\"1\">1</option>" +
							"<option value=\"3\">3</option>" +
							"<option value=\"6\">6</option>" +
							"<option value=\"10\">10</option>" +
							"</select>" +
							"</td>" +
							"<td>" +
							"<label class=\"fowei ftdl\">频次分析</label></td>" +
							"<td>" +
							"<input type=\"text\" class=\"layui-input layui-input1\" id=\"item-frequency-" + i + "-" + j + "-1\" value=\"" + itemArr[i].describeModels[j].frequency + "\" placeholder=\"输入频次分析\" style='margin-right:5px' onchange=\"setItemValue('item-frequency-" + i + "-" + j + "-1'," + i + "," + j + ", 0,1,1,'frequency')\">" +
							"<select lay-ignore class='layui-input layui-unselect'  id=\"item-frequencyNum-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-frequencyNum-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'frequencyNum')\">" +
							"<option value=\"0.1\">0.1</option>" +
							"<option value=\"0.2\">0.2</option>" +
							"<option value=\"0.5\">0.5</option>" +
							"<option value=\"1\">1</option>" +
							"<option value=\"3\">3</option>" +
							"<option value=\"6\">6</option>" +
							"<option value=\"10\">10</option>" +
							"</select>" +
							"</td>" +
							"<td rowspan=\"" + (rightTab) + "\" style=\"vertical-align: middle; padding:0px 9px;\" id=\"item-title-" + i + "-rightBtn\">" +
							"<div class=\"rctd\" onclick=\"addDescribeItem(" + i + ")\"><div>" +
							"<div class=\"rctd-add\"></div>" +
							"<p>增</p><p>加</p><p>风</p>" +
							"<p>险</p><p>分</p><p>析</p>" +
							"</div></div>" +
							"</td>" +
							"</tr>" +
							"<tr id=\"item-probability-k-" + i + "-" + j + "\">" +
							"<td>" +
							"<div class=\"layui-input-inline ftdl\"><label class=\"fowei\">后果分析</label></div>" +
							"</td>" +
							"<td style=\"padding-right: 0px;\">" +
							"<select lay-ignore class=\"result_select layui-input layui-unselect\" id=\"item-aftermath-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-aftermath-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'aftermath')\">" +
							"<option value=\"10人以上死亡\">10人以上死亡</option>" +
							"<option value=\"3～9人死亡\">3～9人死亡</option>" +
							"<option value=\"1～2人死亡\">1～2人死亡</option>" +
							"<option value=\"严重\">严重</option>" +
							"<option value=\"重大，伤残\">重大，伤残</option>" +
							"<option value=\"引人注意\">引人注意</option>" +
							"</select>" +
							"<select lay-ignore class='layui-input layui-unselect' id=\"item-aftermathNum-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-aftermathNum-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'aftermathNum')\">" +
							"<option value=\"1\">1</option>" +
							"<option value=\"3\">3</option>" +
							"<option value=\"7\">7</option>" +
							"<option value=\"15\">15</option>" +
							"<option value=\"30\">30</option>" +
							"<option value=\"100\">100</option>" +
							"</select>" +
							"</td>" +
							"<td>" +
							"<label class=\"fowei ftdl\">风险评估值" +
							"<div>D=L×E×C</div>" +
							"</label>" +
							"</td>" +
							"<td><span class=\"pg_value\">" + itemArr[i].describeModels[j].estimateNum.toFixed(2) + "</span></td>" +
							"</tr>";
					} else {
						contenttab +=
							"<tr id=\"item-probability-title-" + i + "-" + j + "\">" +
							"<td colspan='4' style='padding:9px 0;'>" +
							"<hr class=\"layui-bg-green layui-bg-green\">" +
							"</td>" +
							"<td class=\"delIcon\" onclick=\"delDescribeItem(" + i + "," + j + ")\">" +
							"</td>" +
							"</tr>" +
							"<tr id=\"item-probability-k-" + i + "-" + j + "\">" +
							"<td>" +
							"<div class=\"layui-input-inline ftdl\">" +
							"<label class=\"fowei\">可能性分析</label></div>" +
							"</td>" +
							"<td style=\"padding-right: 0px;\">" +
							"<input type=\"text\" class=\"layui-input layui-input1\" id=\"item-probability-" + i + "-" + j + "-1\" value=\"" + itemArr[i].describeModels[j].probability + "\" placeholder=\"输入可能性分析\" onchange=\"setItemValue('item-probability-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,1,'probability')\"> " +
							"<select lay-ignore class='layui-input layui-unselect' id=\"item-probabilityNum-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-probabilityNum-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'probabilityNum')\">" +
							"<option value=\"0.1\">0.1</option>" +
							"<option value=\"0.2\">0.2</option>" +
							"<option value=\"0.5\">0.5</option>" +
							"<option value=\"1\">1</option>" +
							"<option value=\"3\">3</option>" +
							"<option value=\"6\">6</option>" +
							"<option value=\"10\">10</option>" +
							"</select>" +
							"</td>" +
							"<td>" +
							"<label class=\"fowei ftdl\">频次分析</label></td>" +
							"<td>" +
							"<input type=\"text\" class=\"layui-input layui-input1\" id=\"item-frequency-" + i + "-" + j + "-1\" value=\"" + itemArr[i].describeModels[j].frequency + "\" placeholder=\"输入频次分析\" style='margin-right:5px' onchange=\"setItemValue('item-frequency-" + i + "-" + j + "-1'," + i + "," + j + ", 0,1,1,'frequency')\">" +
							"<select lay-ignore class='layui-input layui-unselect'  id=\"item-frequencyNum-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-frequencyNum-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'frequencyNum')\">" +

							"<option value=\"0.1\">0.1</option>" +
							"<option value=\"0.2\">0.2</option>" +
							"<option value=\"0.5\">0.5</option>" +
							"<option value=\"1\">1</option>" +
							"<option value=\"3\">3</option>" +
							"<option value=\"6\">6</option>" +
							"<option value=\"10\">10</option>" +
							"</select>" +
							"</td>" +
							"<td rowspan=\"" + rightTab + "\" style=\"vertical-align: middle; padding:0px 9px;\" id=\"item-title-" + i + "-rightBtn\">" +
							"<div class=\"rctd\" onclick=\"addDescribeItem(" + i + ")\"><div>" +
							"<div class=\"rctd-add\"></div>" +
							"<p>增</p><p>加</p><p>风</p>" +
							"<p>险</p><p>分</p><p>析</p>" +
							"</div></div>" +
							"</td>" +
							"</tr>" +
							"<tr id=\"item-probability-h-" + i + "-" + j + "\">" +
							"<td>" +
							"<div class=\"layui-input-inline ftdl\"><label class=\"fowei\">后果分析</label></div>" +
							"</td>" +
							"<td style=\"padding-right: 0px;\">" +
							"<select lay-ignore class=\"result_select layui-input layui-unselect\" id=\"item-aftermath-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-aftermath-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'aftermath')\">" +
							"<option value=\"10人以上死亡\">10人以上死亡</option>" +
							"<option value=\"3～9人死亡\">3～9人死亡</option>" +
							"<option value=\"1～2人死亡\">1～2人死亡</option>" +
							"<option value=\"严重\">严重</option>" +
							"<option value=\"重大，伤残\">重大，伤残</option>" +
							"<option value=\"引人注意\">引人注意</option>" +
							"</select>" +
							"<select lay-ignore class='layui-input layui-unselect' id=\"item-aftermathNum-" + i + "-" + j + "-1\" onchange=\"setItemValue('item-aftermathNum-" + i + "-" + j + "-1'," + i + "," + j + ",0,1,2,'aftermathNum')\">" +

							"<option value=\"1\">1</option>" +
							"<option value=\"3\">3</option>" +
							"<option value=\"7\">7</option>" +
							"<option value=\"15\">15</option>" +
							"<option value=\"30\">30</option>" +
							"<option value=\"100\">100</option>" +
							"</select>" +
							"</td>" +
							"<td>" +
							"<label class=\"fowei ftdl\">风险评估值" +
							"<div>D=L×E×C</div>" +
							"</label>" +
							"</td>" +
							"<td><span class=\"pg_value\">" + itemArr[i].describeModels[j].estimateNum.toFixed(2) + "</span></td>" +
							"</tr>";
					}

					contenttab += contentHtml;
				}
			}
			/*-------------检查项目.start-------------*/
			tbodyHtml +=
				"<tr id=\"item-" + i + "\">" +
				"<td class=\"layui-bg-green\">" +
				"<div class=\"layui-input-inline\">" +
				"<label class=\"item-nav\">" + itemArr[i].name + "</label>" +
				"</div>" +
				"</td>" +
				"<td colspan=\"3\" class=\"layui-bg-green\">" +
				"</td>" +
				"<td class=\"delIcon\" onclick=\"delItem(" + i + ")\">" +
				"</td>" +
				"</tr>";
			tbodyHtml += contenttab;
			/*-------------检查项目.end-------------*/
		}
	}
	/*-------------提交按钮.end-------------*/
	tbodyHtml += "<tr>" +
		"<td colspan=\"5\" style=\"text-align: center;\"><button type=\"button\" style=\"width: 100px;font-size: 16px;margin-top: 20px;\" class=\"layui-btn layui-btn-danger\" onclick=\"addSecurityCheck()\">提&nbsp;交</button></td>" +
		"</tr>";
	/*-------------提交按钮.end-------------*/
	$("#addTable").html(tbodyHtml);
	setPosition();

	for(var i = 0; i < itemArr.length; i++) {
		for(var j = 0; j < itemArr[i].describeModels.length; j++) {
			for(var z = 0; z < itemArr[i].describeModels[j].riskInfos.length; z++) {
				set_select_checked("item-type-" + i + "-" + j + "-" + z + "-1", itemArr[i].describeModels[j].riskInfos[z].type);
				set_select_checked("item-measure-" + i + "-" + j + "-" + z + "-1", itemArr[i].describeModels[j].riskInfos[z].measure);
				set_select_checked("item-ent-" + i + "-" + j + "-" + z + "-1", itemArr[i].describeModels[j].riskInfos[z].ent);
			}
			set_select_checked("item-probabilityNum-" + i + "-" + j + "-1", itemArr[i].describeModels[j].probabilityNum);
			set_select_checked("item-frequencyNum-" + i + "-" + j + "-1", itemArr[i].describeModels[j].frequencyNum);
			set_select_checked("item-aftermath-" + i + "-" + j + "-1", itemArr[i].describeModels[j].aftermath);
			set_select_checked("item-aftermathNum-" + i + "-" + j + "-1", itemArr[i].describeModels[j].aftermathNum);
		}
	}

}

function initEditItem(checkInfo) {
	$("#unit").val(checkInfo.unit);
	$("#inventory").val(checkInfo.inventory);
	$("#dexisionTime").val(checkInfo.dexisionTime);
	$("#numberNo").val(checkInfo.numberNo);
	$("#applyUnit").val(checkInfo.entName);
	editCompanyId = checkInfo.entId;

	itemArr = JSON.parse(checkInfo.info);
	initItemShow();
}

/*-------------新增json.start-------------*/
//判断检查项目填写是否正确
function judgeItemArr() {
	for(var i = 0; i < itemArr.length; i++) {
		if(itemArr[i].isShow == 1) {
			var name = itemArr[i].name;
			var emptyNum = 0; //检查内容、评分细则都为空的数量
			var notEmptyNum = 0; //检查内容、评分细则都不为空的数量
			for(var j = 0; j < itemArr[i].riskInfos.length; j++) {
				if(itemArr[i].riskInfos[j].isShow == 1) {
					if(itemArr[i].riskInfos[j].content != "" && itemArr[i].riskInfos[j].rule == "") {
						return false;
					} else if(itemArr[i].riskInfos[j].content == "" && itemArr[i].riskInfos[j].rule != "") {
						return false;
					} else if(itemArr[i].riskInfos[j].content == "" && itemArr[i].riskInfos[j].rule == "") {
						++emptyNum;
					} else {
						++notEmptyNum;
					}
				}
			}
			if(name == "" && notEmptyNum > 0) {
				return false;
			} else if(name != "" && emptyNum == itemArr[i].riskInfos.length) {
				return false;
			} else if(name == "" && i == 0) {
				return false;
			}
		}
	}
	return true;
}

function addSecurityCheck() {

	if(unit == "") {
		layer.msg('请填单元');
		return;
	}
	var applyUnitId = getTreeVal().toString(); //适用单位id
	if(applyUnitId == 0 || applyUnitId == "") {
		if(checkId != false) { //编辑
			applyUnitId = editCompanyId;
		} else {
			layer.msg('请选择适用单位');
			return;
		}
	}
	var unit = $.trim($("#unit").val()); //单元
	var inventory = $("#inventory").val(); //清单
	var dexisionTime = $("#dexisionTime").val(); //选择时间
	var numberNo = $("#numberNo").val(); //编号
	var dexisionTime = $("#dexisionTime").val(); //选择时间

	//	var itemBool = judgeItemArr();
	//	if(!itemBool) {
	//		layer.msg('请填写完整检查项目');
	//		return;
	//	}
	//	if(checkId == false) { //新增时
	//		checkId = "";
	//	}
	var postUrl;
	if($('#id').val() == '') {
		postUrl = url + 'risk/add'
	} else {
		postUrl = url + 'risk/edit'
	}

	$.ajax({
		url: postUrl,
		dataType: 'json',
		async: false,
		type: "POST",
		data: {
			id: $('#id').val(),
			Token: salt,
			checkId: checkId,
			unit: unit,
			entId: applyUnitId,
			inventory: inventory,
			dexisionTime: dexisionTime,
			share: maxEstimateNum,
			number: numberNo,
			info: JSON.stringify(itemArr)
		},
		success: function(data) {
			if(data.code == 0) {
				var str = "新增提交成功！";
				if(checkId != "") {
					str = "修改提交成功！";
				}
				layer.confirm(str, {
					btn: ['确定'] //按钮
				}, function() {
					location.href = "../list/list.html";
				}, function() {});
			} else {
				layer.msg(data.msg);
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
}
/*-------------新增安全检查表.end-------------*/
//项目内容赋值
function setItemValue(id, itemOrder, describeModelsOrder, riskInfosOrder, indexOrder, type, key) {
	if(indexOrder == 1) {
		if(type == 1) {
			if(key == 'probability') {
				itemArr[itemOrder].describeModels[describeModelsOrder].probability = $("#" + id).val();
			}
			if(key == 'frequency') {
				itemArr[itemOrder].describeModels[describeModelsOrder].frequency = $("#" + id).val();
			}

		} else {
			if(key == 'probabilityNum') {
				itemArr[itemOrder].describeModels[describeModelsOrder].probabilityNum = $("#" + id + " option:selected").val();
			}
			if(key == 'frequencyNum') {
				itemArr[itemOrder].describeModels[describeModelsOrder].frequencyNum = $("#" + id + " option:selected").val();
			}
			if(key == 'aftermath') {
				itemArr[itemOrder].describeModels[describeModelsOrder].aftermath = $("#" + id + " option:selected").val();
			}
			if(key == 'aftermathNum') {
				itemArr[itemOrder].describeModels[describeModelsOrder].aftermathNum = $("#" + id + " option:selected").val();
			}

			estimate = itemArr[itemOrder].describeModels[describeModelsOrder].probabilityNum * itemArr[itemOrder].describeModels[describeModelsOrder].frequencyNum * itemArr[itemOrder].describeModels[describeModelsOrder].aftermathNum;

			if(estimate > maxEstimateNum) {
				maxEstimateNum = estimate;
			}
			itemArr[itemOrder].describeModels[describeModelsOrder].estimateNum = estimate;
		}

	}
	if(indexOrder == 2) {
		if(type == 1) {
			if(key == 'analyse') {
				itemArr[itemOrder].describeModels[describeModelsOrder].riskInfos[riskInfosOrder].analyse = $("#" + id).val();
			}
			if(key == 'responsible') {
				itemArr[itemOrder].describeModels[describeModelsOrder].riskInfos[riskInfosOrder].responsible = $("#" + id).val();
			}
			if(key == "specific") {
				itemArr[itemOrder].describeModels[describeModelsOrder].riskInfos[riskInfosOrder].specific = $("#" + id).val();
			}
		} else {
			if(key == 'type') {
				itemArr[itemOrder].describeModels[describeModelsOrder].riskInfos[riskInfosOrder].type = $("#" + id + " option:selected").val();
			}
			if(key == 'measure') {
				itemArr[itemOrder].describeModels[describeModelsOrder].riskInfos[riskInfosOrder].measure = $("#" + id + " option:selected").val();
			}
			if(key == 'ent') {
				itemArr[itemOrder].describeModels[describeModelsOrder].riskInfos[riskInfosOrder].ent = $("#" + id + " option:selected").val();
			}
		}
	}

	initItemShow();
}

//删除yi级内容
function delItem(temOrder) {
	var showNum = getShowItemTwoNum(temOrder);
	if(itemArr[temOrder].describeModels.length > 0) {
		delDescribeItem(temOrder, 0);
	} else {
		for(var i = 0; i < itemArr.length; i++) {
			var maxIndex = itemArr.length - 1;
			if(i >= temOrder && i < maxIndex) {
				itemArr[i] = itemArr[i + 1];
			}
		}
		itemArr.pop();
		initItemShow();
	}

}

//获取二级长度
function getShowItemTwoNum(temOrder) {
	var showNum = 0;
	for(var i = 0; i < itemArr[temOrder].describeModels.length; i++) {
		if(itemArr[temOrder].describeModels[i].isShow == 1) {
			++showNum;
		}
	}
	return showNum;
}

//增加二级内容
function addDescribeItem(temOrder) {
	var showNum = getShowItemTwoNum(temOrder);
	itemArr[temOrder].describeModels[showNum] = {
		isShow: 1,
		probability: "",
		probabilityNum: 0.1,
		frequency: "",
		frequencyNum: 0.1,
		aftermath: "",
		aftermathNum: 1,
		estimateNum: 0.01,
		riskInfos: [{
			isShow: 1,
			type: "人的不安全行为",
			analyse: "",
			measure: "安全管理措施",
			specific: "",
			ent: "厂",
			responsible: ""
		}]
	};
	initItemShow();
}

function delDescribeItem(temOrder, describeOrder) {
	var showNum = getShowItemTwoNum(temOrder);
	for(var i = 0; i < itemArr[temOrder].describeModels.length; i++) {
		var maxIndex = itemArr[temOrder].describeModels.length - 1;
		if(i >= describeOrder && i < maxIndex) {
			itemArr[temOrder].describeModels[i] = itemArr[temOrder].describeModels[i + 1];
		}
	}
	itemArr[temOrder].describeModels.pop();
	initItemShow();
}

//获取检查内容数量
function getShowContent(itemOrder, describeOrder) {
	var showNum = 0;
	for(var i = 0; i < itemArr[itemOrder].describeModels[describeOrder].riskInfos.length; i++) {
		if(itemArr[itemOrder].describeModels[describeOrder].riskInfos[i].isShow == 1) {
			++showNum;
		}
	}
	return showNum;
}

//删除内容框
function delContent(itemOrder, describeOrder, riskInfoOrder) {

	var showNum = getShowContent(itemOrder, describeOrder);
	if(showNum == 1) {
		return;
	}
	for(var i = 0; i < itemArr[itemOrder].describeModels[describeOrder].riskInfos.length; i++) {
		var maxIndex = itemArr[itemOrder].describeModels[describeOrder].riskInfos.length - 1;
		if(i >= riskInfoOrder && i < maxIndex) {
			itemArr[itemOrder].describeModels[describeOrder].riskInfos[i] = itemArr[itemOrder].describeModels[describeOrder].riskInfos[i + 1];
		}
	}
	itemArr[itemOrder].describeModels[describeOrder].riskInfos.pop();
	initItemShow();
}

//增加内容框
function addContent(itemOrder, describeOrder, z) {

	var showNum = getShowContent(itemOrder, describeOrder);
	itemArr[itemOrder].describeModels[describeOrder].riskInfos.splice(z + 1, 0, {
		isShow: 1,
		type: "人的不安全行为",
		analyse: "",
		measure: "安全管理措施",
		specific: "",
		ent: "厂",
		responsible: ""
	});
	initItemShow(true);
}

var riskbm
//选中框变色
function addriskbm(valtext) {}

function riskbtm(exportType) {
	$(".model").show()
}

function getShowItemNum() {
	var showNum = 0;
	for(var i = 0; i < itemArr.length; i++) {
		if(itemArr[i].isShow == 1) {
			++showNum;
		}
	}
	return showNum;
}

$("body").on("click", ".model .content li", function() {
	$(this).toggleClass("active");
	var text = $(this).text();
	if($(this).hasClass("active")) {
		var showNum = getShowItemNum();
		itemArr[showNum] = {
			isShow: 1,
			name: text,
			describeModels: [{
				isShow: 1,
				probability: "",
				probabilityNum: 0.1,
				frequency: "",
				frequencyNum: 0.1,
				aftermath: "",
				aftermathNum: 1,
				estimateNum: 0.01,
				riskInfos: [{
					isShow: 1,
					type: "人的不安全行为",
					analyse: "",
					measure: "安全管理措施",
					specific: "",
					ent: "厂",
					responsible: ""
				}]
			}]
		};
		initItemShow();
	} else {
		for(var i = 0; i < itemArr.length; i++) {
			if(itemArr[i].name == text) {
				delItem(i);
			}

		}
		//		app.list.forEach(function(item, index) {
		//			if(item.title == text) {
		//				app.list.splice(index, 1)
		//			}
		//		})
	}
});

$("body").on("click", ".model .button button", function() {
	$(".model").hide()
});

//获取企业单位信息
function getAllCompany() {
	$.ajax({
		url: url + 'baseEnt/getAllEnt',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {},
		success: function(data) {
			$('#applyUnitTree').empty();
			if(data.code == 0) {
				company = data.data;
			}
		},
		error: function(error) {
			console.log("获取失败");
		}
	});
}